package com.test.gpa.test.suanfa;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class A {


    public void nextPermutation(int[] nums) {
        if (nums.length <= 1) return;

        int num = nums.length - 2;
        while (num >= 0 && nums[num] >= nums[num + 1]) {
            num--;
        }

        if (num >= 0) {
            int i = nums.length - 1;
            while (i >= 0 && nums[num] >= nums[i]) {
                i--;

            }
            swap(nums, num, i);
        }
        revers(nums, num + 1, nums.length - 1);

    }

    public static void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;

    }

    public void revers(int[] nums, int i, int j) {
        while (i < j) {
            swap(nums, i, j);
            i++;
            j--;

        }


    }

    public static int[] revers1(int[] nums, int i, int j) {
        while (i < j) {
            swap(nums, i, j);
            i++;
            j--;

        }
        return nums;

    }


    public int nextGreaterElement(int n) {
        char[] chars = String.valueOf(n).toCharArray();
        int length = chars.length;
        if (length <= 1) return -1;

        int i = length - 2;
        while (i >= 0 && chars[i] >= chars[i + 1]) {
            i--;
        }
        if (i < 0) {
            return -1;
        }

        int j = length - 1;
        while (j >= 0 && chars[i] >= chars[j]) {
            j--;
        }
        swap(chars, i, j);

        revers(chars, i + 1);
        Long a = Long.valueOf(String.valueOf(chars));
        return a > Integer.MAX_VALUE ? -1 : Math.toIntExact(a);

    }

    public void swap(char[] chars, int i, int j) {
        char temp = chars[i];
        chars[i] = chars[j];
        chars[j] = temp;
    }

    public void revers(char[] chars, int j) {
        int k = chars.length - 1;
        while (j < k) {
            swap(chars, j, k);
            j++;
            k--;
        }
    }

    public static String replaceWords(List<String> dictionary, String sentence) {
        String[] split = sentence.split(" ");
        StringBuilder stringBuilder = new StringBuilder();
        List<String> collect = dictionary.stream().sorted().collect(Collectors.toList());
        dictionary = collect;
        for (String a :
                split) {
            for (int i = 0; i < dictionary.size(); i++) {
                if (a.startsWith(dictionary.get(i))) {
                    stringBuilder.append(dictionary.get(i) + " ");
                    break;
                } else {
                    if (i == dictionary.size() - 1) stringBuilder.append(a + " ");
                }
            }
        }
        return stringBuilder.substring(0, stringBuilder.length() - 1);
    }


    public static void main(String[] args) {
//        List<String> dictionary = new ArrayList<>();
//        dictionary.add("cat");
//        dictionary.add("bat");
//        dictionary.add("rat");
//        String a = "the cattle was rattled by the battery";
//        replaceWords(dictionary, a);
        System.out.println(divide(2000,-1));
    }

    public static int divide(int a, int b) {
        if(a==0||b==0) return 0;
        int g=1;
        if(a*b<0){   a=Math.abs(a); b=Math.abs(b);g=0;}
        int c = 1;
        for (int i = 0; i <= a; i++) {
            int d = a - b;
            if (a < d) {
                break;
            }
            a=d;
            c++;
        }
        if(g==0) return -c;
        return c;
    }
}
